package cn.yanglq.Rss.web.controller.jobset;

import cn.yanglq.Rss.web.common.vo.ApiResult;
import cn.yanglq.Rss.web.controller.BaseController;
import cn.yanglq.Rss.web.model.JobSet;
import cn.yanglq.Rss.web.model.MailSet;
import cn.yanglq.Rss.web.service.JobSetService;
import cn.yanglq.Rss.web.service.MailSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * Created by yanglq on 2018/1/17.
 */
@Controller
@RequestMapping("/jobset")
public class JobSetController extends BaseController {
    @Autowired
    private JobSetService jobSetService;
    @Autowired
    private MailSetService mailSetService;

    @RequestMapping("/jobsetpage")
    public String jobsetpage() {
        return "/jobset/jobinfolist";
    }

    @RequestMapping("/addJobPage")
    public ModelAndView addJobPage() {
        String Loginid = findCurrentLoginid();
        List<MailSet> sets = mailSetService.selectMailInfos(Loginid);
        ModelAndView mv = new ModelAndView();
        mv.addObject("sets", sets);
        mv.setViewName("/jobset/addjobinfo");
        return mv;
    }

    @RequestMapping("/jobsearch")
    public ModelAndView jobsearch() {

        String Loginid = findCurrentLoginid();
        List<JobSet> list = jobSetService.selectjobInfos(Loginid);
        List<MailSet> sets = mailSetService.selectMailInfos(Loginid);
        ModelAndView mv = new ModelAndView();
        mv.addObject("list", list);
        mv.addObject("sets", sets);
        mv.setViewName("/jobset/jobinfosearchlist");
        return mv;
    }


    @RequestMapping("/deleteJob")
    @ResponseBody
    public ApiResult deleteJob(@RequestParam(value = "id", required = true) Integer id) {
        ApiResult result = new ApiResult();
        jobSetService.deleteJob(id);
        result.setSuccess(true);
        result.setErrorcode(0);
        result.setMessage("删除成功！");
        return result; //校验通过返回成功
    }


    @RequestMapping("/addJob")
    @ResponseBody
    public ApiResult addJob(@RequestParam(value = "sendid", required = true) Integer sendid,
                            @RequestParam(value = "year", required = true) Integer year,
                            @RequestParam(value = "month", required = true) Integer month,
                            @RequestParam(value = "week", required = true) Integer week,
                            @RequestParam(value = "day", required = true) Integer day,
                            @RequestParam(value = "hour", required = true) Integer hour,
                            @RequestParam(value = "minute", required = true) Integer minute,
                            @RequestParam(value = "second", required = true) Integer second) {
        ApiResult result = new ApiResult();
        JobSet jobinfo = new JobSet();
        String userid = findCurrentLoginid();
        jobinfo.setSendid(sendid);
        jobinfo.setYear(year);
        jobinfo.setMonth(month);
        jobinfo.setWeek(week);
        jobinfo.setDay(day);
        jobinfo.setHour(hour);
        jobinfo.setMinute(minute);
        jobinfo.setSecond(second);
        jobinfo.setUserid(userid);
        jobinfo.setCreater(userid);
        jobinfo.setCreatetime(new Date());
        jobSetService.addJobInfo(jobinfo);
        result.setSuccess(true);
        result.setErrorcode(0);
        result.setMessage("新增成功！");
        return result; //校验通过返回成功
    }

}
